﻿using System;
using System.Collections.Generic;
using SCRUM.Core.DAO;

namespace SCRUM.Core.BusinessObjects
{
    /// <summary>
    /// Object representing Backlog Item in Scrum methodology
    /// Holds following information:
    ///  * ID - object's unique identifier (among objects of same type)
    ///  * Name - name of object
    ///  * Estimation - estimation of total work on object
    ///  * Progress - progress of total work on object (in%)
    ///  * LastChange - date of last changes made to object
    ///  * Description - description of object
    ///  * Priority - priority of object (among objects of same type)
    ///  * UserStoryID - identifier of superior user story
    ///  * TypeID - identifier of type of user story (1 = technical, 2=functional, 3=other)
    ///  * State - State of the Backlog Item
    /// Offers these lazy loading methods:
    ///  * Tasks - list of all inferior tasks from which consits this backlog item
    ///  * UserSotry - superior UserStory business object
    /// </summary>
    [Serializable()]
    public class BLI : PriorityBusinessObject
    {
        /// <summary>
        /// Gets or sets the user story id.
        /// </summary>
        /// <value>The user story id.</value>
        public int UserStoryID { get; set; }

        /// <summary>
        /// Gets or sets the type ID.
        /// </summary>
        /// <value>The type ID.</value>
        public int TypeID { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="BLI"/> class.
        /// </summary>
        /// <param name="id">Identifier of BLI.</param>
        /// <param name="name">Name of BLI.</param>
        /// <param name="typeID">TypeID of BLI.</param>
        /// <param name="desc">Description of BLI.</param>
        /// <param name="userStoryID">ID of the user story.</param>
        /// <param name="stateID">Current state of BLI.</param>
        public BLI(int id, string name, int typeID, string desc, int userStoryID, int stateID, double estimation, double progress, int priority, DateTime change)
            : base(id, name, desc, estimation, progress, priority, change,stateID)
        {
            this.TypeID = typeID;
            this.UserStoryID = userStoryID;
            this.stateType = State.StateTypes.Bli;
        }

        public override string SystemID
        {
            get
            {
                return MakeSystemID("B",this.ID);
            }
            set {}
        }

        /// <summary>
        /// Gets or sets the superior item's ID.
        /// </summary>
        /// <value>The superior item ID.</value>
        public override int SuperiorItemID
        {
            get { return this.UserStoryID; }
            set { this.UserStoryID = value; }
        }

        /// <summary>
        /// Gets or sets the user story.
        /// </summary>
        /// <value>The user story.</value>
        public UserStory UserStory
        {
            get
            {
                // Lazy loading
                if (this.userStory == null)
                {
                    this.userStory = DAOUserStory.GetUserStoryByBliID(this.ID);
                }
                return this.userStory;
            }
            set
            {
                this.userStory = value;
            }
        }

        /// <summary>
        /// Gets or sets the name of the user story.
        /// </summary>
        /// <value>The name of the user story.</value>
        public string UserStoryName
        {
            get 
            {
                UserStory us = this.UserStory;
                return us == null ? "" : us.Name;
            }
            set { } 
        }

        /// <summary>
        /// Gets or sets the tasks.
        /// </summary>
        /// <value>The tasks.</value>
        public IList<Task> Tasks
        {
            get
            {
                // Lazy loading
                if (this.tasks == null)
                {
                    this.tasks = DAOTask.GetAllTasksForBli(this);
                }
                return this.tasks;
            }
            set
            {
                this.tasks = value;
            }
        }

        /// <summary>
        /// User story that contains this BLI.
        /// </summary>
        private UserStory userStory;

        /// <summary>
        /// Tasks associated to this BLI.
        /// </summary>
        private IList<Task> tasks;
    }
}
